package com.cicoding.controller;

import com.cicoding.annotation.BizLog;
import com.cicoding.bean.File;
import com.cicoding.core.ResponseResult;
import com.cicoding.service.FileService;
import com.cicoding.utils.ResponseResultUtil;
import com.google.gson.Gson;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
@RequestMapping("/file")
public class FileController {
    @Autowired
    private FileService fileService;
    @Autowired
    private Gson gson;
    private Logger log = LoggerFactory.getLogger(this.getClass());


    @BizLog("添加通知")
    @ResponseBody
    @PostMapping(value = "/add.json")
    public ResponseResult addFile(File file) {
        log.info("{}",file);
        fileService.insert(file);

        return ResponseResultUtil.renderSuccess(file);
    }


    @GetMapping(value = "/fileList.htm")
    public String fileListPage(Integer id,HttpServletRequest request) {
        List<File> list = fileService.selectList(null);
        request.setAttribute("filelist", list);
        return "file/fileList";
    }

    @RequestMapping(value = "/{id}.htm", method = {RequestMethod.GET})
    public String filePage(@PathVariable("id") Integer id, HttpServletRequest request) {
        File file = fileService.selectById(id);
        request.setAttribute("file", file);
        return "file/file";
    }

    @RequestMapping(value = "/addFile.htm", method = {RequestMethod.GET})
    public String filePage(HttpServletRequest request) {
        log.info("file page");
        return "file/addFile";
    }
}
